package com.mayflygeek.mayflyask.framework.webmvc.message;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Preconditions;

/**
 * @author 虫虫
 * @team 蜉蝣极客
 * @link www.mayflygeek.com
 * @Emial chongchong@mayflygeek.com
 * 
 */
public class FlashImpl implements Flash, Serializable {
	private static final long serialVersionUID = 624528874280233112L;
	private final Map<String, Object> attributes;

	public FlashImpl() {
		this(new HashMap<String, Object>());
	}

	public FlashImpl(Map<String, Object> attributesContainer) {
		attributes = Preconditions.checkNotNull(attributesContainer);
	}

	@Override
	public void setAttribute(String key, Object value) {
		attributes.put(key, value);
	}

	@Override
	public Object getAttribute(String key) {
		return attributes.get(key);
	}

	@Override
	public Map<String, Object> getAttributes() {
		return attributes;
	}

	@Override
	public void setMessage(Message message) {
		setAttribute(MESSAGE_KEY, message);
	}

	@Override
	public void setInfoMessage(String info) {
		setMessage(new Message(MessageType.info, info));
	}

	@Override
	public void setWarningMessage(String warning) {
		setMessage(new Message(MessageType.warning, warning));
	}

	@Override
	public void setErrorMessage(String error) {
		setMessage(new Message(MessageType.error, error));
	}

	@Override
	public void setSuccessMessage(String success) {
		setMessage(new Message(MessageType.success, success));
	}

	@Override
	public Message getMessage() {
		return (Message) attributes.get(MESSAGE_KEY);
	}

	@Override
	public boolean hasMessage() {
		return getMessage() != null;
	}
}
